﻿import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

ColumnLayout{

    property string targetId: ""
    property string targetType: "10"//album,palylsit/detail
    property string name: "-"

    onTargetIdChanged:
    {
        console.log(targetType)
        if(targetType==="10")loadAlbum()
        else if(targetType==="1000")loadPlayList()
    }


    Rectangle{
        Layout.fillWidth: true
        width: parent.width
        height: 60
        color: "#00000000"
        Text{
            x:10
            verticalAlignment: Text.AlignBottom
            text: qsTr(targetType=="10"?"专辑":"歌单")+name
            font.family: window.mFONT_FAMILY
            font.pointSize: 25
            color:"white"
        }
    }


    RowLayout{
        height: 200
        width:parent.width
        MusicBorderImage{
            id:playListCover
            width:180
            height:180
            imgSrc: "https://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg"


        }
        Item{
            Layout.fillWidth: true
            height: parent.height
            Text{
                id:playListDesc
                text:"https://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpghttps://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpghttps://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpghttps://p1.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg"
                width:parent.width
                anchors.centerIn: parent
                wrapMode: Text.WrapAnywhere
                font.family: window.mFONT_FAMILY
                font.pointSize: 14
                maximumLineCount: 4
                elide: Text.ElideRight
                lineHeight: 1.5
                color:"white"
            }
        }
    }
    MusicListView{
        id:playListListView
        deleteble: false
    }
    function loadAlbum(){
            loading.open()
            var url = "album?id="+(targetId.length<1?"32311":targetId)

            function onReply(reply){
                loading.close()
                http.onReplySignal.disconnect(onReply)
                try{
                    if(reply.length <1){
                        notification.openError("获取专辑列表结果为空！")

                        return
                    }
                var album = JSON.parse(reply).album
                var songs = JSON.parse(reply).songs
                playListCover.imgSrc = album.blurPicUrl
                playListDesc.text = album.description
                name = "-"+album.name
                playListListView.musicList= songs.map(item=>{
                                                          return {
                                                              id:item.id,
                                                              name:item.name,
                                                              artist:item.ar[0].name,
                                                              album:item.al.name,
                                                              cover:item.al.picUrl
                                                          }
                                                      })
                }catch(err){
                    notification.openError("获取专辑列表出错！")

                }
            }

            http.onReplySignal.connect(onReply)
            http.connect(url)
        }

        function loadPlayList(){
                loading.open()
            var url = "playlist/detail?id="+(targetId.length<1?"32311":targetId)


            function onSongDetailReply(reply){
                loading.close()
                http.onReplySignal.disconnect(onSongDetailReply)
                try{
                    if(reply.length <1){
                        notification.openError("获取歌单列表详情结果为空！")

                        return
                    }
                var songs = JSON.parse(reply).songs
                playListListView.musiclist= songs.map(item=>{
                                                          return {
                                                              id:item.id,
                                                              name:item.name,
                                                              artist:item.ar[0].name,
                                                              album:item.al.name,
                                                              cover:item.al.picUrl

                                                          }
                                                      })
                }catch(err){
                    notification.openError("获取歌单列表详情出错！")

                }
            }

            function onReply(reply){
                loading.close()
                http.onReplySignal.disconnect(onReply)
                try{
                    if(reply.length <1){
                        notification.openError("获取歌单列表结果为空！")

                        return
                    }
                var playlist = JSON.parse(reply).playlist
                playListCover.imgSrc = playlist.coverImgUrl
                playListDesc.text = playlist.description
                name = "-"+playlist.name
                var ids = playlist.trackIds.map(item=>item.id).join(",")
                http.onReplySignal.connect(onSongDetailReply)
                http.connect("song/detail?ids="+ids)
                }catch(err){
                    notification.openError("获取歌单列表出错！")

                }
            }
            http.onReplySignal.connect(onReply)
            http.connect(url)
        }
}


